limsArchivesManage.editPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: _this.idManager.archivesGrid,
            url: _this.urlManager.archivesGrid + '/queries',
            service: 'limsArchivesManageServiceImpl',
            dbTable: 'T_LIMS_ARCHIVES_MANAGE',
            columns: limsArchivesManage.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'archivesCode' && _this.loadDetailPage(row.archivesCode);
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                }
            },
            onRowActive: function (index, row) {
            },
            page: true,
            deleteFormatter: function (row) {
                return row.archivesCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.ARCHIVESMANAGES.MODAL.ADD',
                        fields: limsArchivesManage.getBaseInfoFormFields(),
                        url: limsArchivesManage.baseUrl,
                        disableNotEmptyValidate: true,
                    }).done(function (id) {
                        var url = Gikam.printf(limsArchivesManage.baseUrl + '/{id}', {
                            id: id
                        })
                        var row = {}
                        Gikam.getTextSync(url).done(data => {
                            row = JSON.parse(data)
                        })
                        if (row.archivesCode) {
                            _this.loadDetailPage(row.archivesCode)
                        }
                        Gikam.getComp('lims-archives-manage-edit-list-grid').refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-archives-manage-edit-list-grid').deleteRows(limsArchivesManage.baseUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORT',
                icon: 'export',
                onClick: function () {
                    Gikam.getText(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                        templateCode: 'LimsArchivesExportTemplate',
                        service: 'LimsArchivesExportTemplate'
                    })).done(function (url) {
                        Gikam.download(url)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORT',
                icon: 'import',
                onClick: function () {
                    Gikam.create('simpleUploader', {
                        id: 'Archives-info-uploader',
                        dbTable: 'T_LIMS_ARCHIVES_MANAGE',
                        bizId: 'self',
                        autoClose: true,
                        multiple: false,
                        accept: ['xls'],
                        onAfterClose: function (fields) {
                            if (Gikam.isNotEmpty(fields)) {
                                workspace.window.showMask()
                                let url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsArchivesImportFileFunction', {
                                    id: fields[0].id
                                })
                                Gikam.post(url).done(req => {
                                    Gikam.getComp(_this.idManager.archivesGrid).refresh()
                                    if (req.message){
                                        Gikam.alert(req.message)
                                    }
                                    if (req.url){
                                        Gikam.download(req.url);
                                    }
                                }).always(function () {
                                    workspace.window.closeMask()
                                })
                            }
                        }
                    })
                }
            }]
        }
    },

    idManager: {
        archivesGrid: 'lims-archives-manage-edit-list-grid',
        archivesBorrowGrid: 'lims-archives-handle-edit-list-grid' + '-borrow',
        archivesMoveGrid: 'lims-archives-handle-edit-list-grid' + '-move',
        archivesDestoryGrid: 'lims-archives-handle-edit-list-grid' + '-destory',
    },

    urlManager: {
        archivesGrid: limsArchivesManage.baseUrl,
        archivesHandleGrid: IFM_CONTEXT + '/secure/basemodule/rm/archives-handles',
    },

    loadDetailPage: function (archivesCode) {
        workspace.window.load(limsArchivesManage.baseUrl + '/page/detail' + Gikam.param({
            archivesCode: archivesCode
        }));
    },

    create: function () {
        let _this = this;
        Gikam.create('layout', {
            id: 'lims-archives-manage-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [_this.getGridParam()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
